import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:sui_shou_pai/Utils/storage_util.dart';
import 'package:sui_shou_pai/network/http_config.dart';
import 'package:sui_shou_pai/network/http_request.dart';
import 'package:sui_shou_pai/news/announcement/announcement_list_item.dart';

import '../data/announcement_list_item_data.dart';

class AnnouncementList extends StatefulWidget {
  AnnouncementList({
    Key key,
  }) : super(key: key);

  @override
  AnnouncementListState createState() => AnnouncementListState();
}

class AnnouncementListState extends State<AnnouncementList> {
  List<dynamic> tempList = [];
  List<AnnouncementListItemData> announcementList = [];

  @override
  void initState() {
    super.initState();
    Future.delayed(
        Duration.zero,
        () => setState(() {
              initData();
            }));
  }

  initData() async {
    HttpRequest.request("$BASE_URL/announcement/announcementList",
            method: "GET", token: await StorageUtil.getStringItem("token"))
        .then((value) {
      setState(() {
        tempList = json.decode(value.toString())['result'];
      });
      for (int i = 0; i < tempList.length; i++) {
        announcementList.add(AnnouncementListItemData.fromMap(tempList[i]));
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: announcementList.length,
      itemBuilder: (BuildContext context, int index) {
        return initAnnouncementListItem(context, announcementList[index]);
      },
    );
  }
}
